// $codepro.audit.disable
package edu.gatech.oad.cyberdoc.view;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import edu.gatech.oad.cyberdoc.presenter.Presenter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

import org.json.JSONObject;

import java.awt.Color;

@SuppressWarnings("serial")
public class LoginComponentsPanel extends ApplicationPanel {
	private JTextField usernameField;
	private JTextField passwordField;
	private JLabel lblError;

	/**
	 * Create the panel.
	 */
	public LoginComponentsPanel() {
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);

		ActionHandler handler = new ActionHandler();

		JLabel lblUsername = new JLabel("Username:");
		add(lblUsername);

		usernameField = new JTextField();
		usernameField.addActionListener(handler);
		springLayout.putConstraint(SpringLayout.WEST, usernameField, 82,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH, lblUsername, 6,
				SpringLayout.NORTH, usernameField);
		springLayout.putConstraint(SpringLayout.EAST, lblUsername, -6,
				SpringLayout.WEST, usernameField);
		springLayout.putConstraint(SpringLayout.NORTH, usernameField, 10,
				SpringLayout.NORTH, this);
		add(usernameField);
		usernameField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.addActionListener(handler);
		springLayout.putConstraint(SpringLayout.NORTH, passwordField, 6,
				SpringLayout.SOUTH, lblUsername);
		springLayout.putConstraint(SpringLayout.WEST, passwordField, 0,
				SpringLayout.WEST, usernameField);
		add(passwordField);
		passwordField.setColumns(10);

		JLabel lblPassword = new JLabel("Password:");
		springLayout.putConstraint(SpringLayout.NORTH, lblPassword, 6,
				SpringLayout.NORTH, passwordField);
		springLayout.putConstraint(SpringLayout.EAST, lblPassword, 0,
				SpringLayout.EAST, lblUsername);
		add(lblPassword);

		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(handler);
		springLayout.putConstraint(SpringLayout.NORTH, btnLogin, 2,
				SpringLayout.SOUTH, passwordField);
		springLayout.putConstraint(SpringLayout.WEST, btnLogin, 0,
				SpringLayout.WEST, usernameField);
		add(btnLogin);

		lblError = new JLabel("");
		lblError.setForeground(Color.RED);
		springLayout.putConstraint(SpringLayout.NORTH, lblError, 41,
				SpringLayout.SOUTH, lblPassword);
		springLayout.putConstraint(SpringLayout.WEST, lblError, 0,
				SpringLayout.WEST, lblUsername);
		add(lblError);
	}

	public void setError(String error) {
		lblError.setText(error);
	}

	private class ActionHandler implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			final String username = usernameField.getText();
			final String password = passwordField.getText();
			
			passwordField.setText("");
			lblError.setText("");

			final Presenter presenter = Presenter.getInstance();
			presenter.login(username, password);
		}
	}

	@Override
	public void show(JSONObject response) {
		// TODO Auto-generated method stub
		
	}
}
